package org.casaca.checkbackup.dbaccess;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Set;

public class Server {
	
	private static Server server;
	static{
		server = null;
	}
	
	public static Server getInstance(){
		if(server==null)
			server = new Server();
		
		return server;
	}
	
	private org.h2.tools.Server tcpServer;
	private org.h2.tools.Server webServer;
	private Properties tcpProperties;
	private Properties webProperties;
	
	private Server(){
		this.tcpServer = webServer = null;
		this.tcpProperties = this.webProperties = null;
	}
	
	public Properties getTCPProperties() throws FileNotFoundException, IOException{
		if(this.tcpProperties==null){
			this.tcpProperties = new Properties();
			File tcpPropFile = new File(System.getProperty("user.home")+System.getProperty("file.separator")+"CheckBackup"+System.getProperty("file.separator")+"TCPServer.properties");
			if(tcpPropFile.exists()){
				this.tcpProperties.load(new FileInputStream(tcpPropFile));
			}
			else{
				this.tcpProperties.load(Server.class.getResourceAsStream("defaultTCPServer.properties"));
			}
		}
		
		return this.tcpProperties;
	}
	
	public Properties getWebProperties() throws FileNotFoundException, IOException{
		if(this.webProperties==null){
			this.webProperties = new Properties();
			File webPropFile = new File(System.getProperty("user.home")+System.getProperty("file.separator")+"CheckBackup"+System.getProperty("file.separator")+"WebServer.properties");
			if(webPropFile.exists()){
				this.webProperties.load(new FileInputStream(webPropFile));
			}
			else{
				this.webProperties.load(Server.class.getResourceAsStream("defaultWebServer.properties"));
			}
		}
		
		return this.webProperties;
	}

	public void startTCPServer() throws IOException, SQLException{
		if(this.tcpServer!=null)
			throw new IllegalStateException("TCP server is started");
		
		Properties tcpProp = this.getTCPProperties();
		String[] args = new String[tcpProp.size()*2];
		Set<Object> e= tcpProp.keySet();
		int i=-1;
		for (Object o : e) {
			args[++i] = String.valueOf(o);
			args[++i] = tcpProp.getProperty(String.valueOf(o));
		}
		
		this.tcpServer = org.h2.tools.Server.createTcpServer(args);
	}
	
	public void startWebServer() throws IOException, SQLException{
		if(this.webServer!=null)
			throw new IllegalStateException("Web server is started");
		
		Properties webProp = this.getWebProperties();
		String[] args = new String[webProp.size()*2];
		Set<Object> e= webProp.keySet();
		int i=-1;
		for (Object o : e) {
			args[++i] = String.valueOf(o);
			args[++i] = webProp.getProperty(String.valueOf(o));
		}
		
		this.tcpServer = org.h2.tools.Server.createWebServer(args);
	}
	
	public void stopTCPServer(){
		if(this.tcpServer!=null)
			throw new IllegalStateException("TCP server is not started");
		
		this.tcpServer.stop();
	}
	
	public void stopWebServer(){
		if(this.webServer!=null)
			throw new IllegalStateException("Web server is not started");
		
		this.webServer.stop();
	}
}